<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          肥料信息系统
        </div>
        <div class="w2">
          在肥料信息系统中你可以查看肥料等基本信息，同时还可以实现修改肥料信息，添加肥料信息等功能，提供一套可行的肥料信息方案
        </div>
        <div class="w2">
          肥料信息系统是种植系统中重要的一环,它可以为我们提供肥料的信息，为下一步做准备
        </div>
      </div>
      <img src="../../static/u=2179590687,2836296523&fm=253&fmt=auto&app=138&f=JPEG.webp" class="imgDri">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="outerVisible = true">新增肥料信息</el-button>
        <el-input v-model="input3" placeholder="请输入要查询的数据" class="searchIuput">
          <el-select slot="prepend" v-model="select" class="sel3" placeholder="请选择" style="width: 120px;">
            <el-option label="是否有机" value="isOrganic" />
            <el-option label="肥料名称" value="fertilizerName" />
          </el-select>
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>
      <el-dialog title="新增肥料信息表" :visible.sync="outerVisible">
        <el-form class="form">
          <el-form-item class="item" label="肥料名称" :label-width="formLabelWidth">
            <el-input v-model="addMes.fertilizerName" class="in" autocomplete="off" />
          </el-form-item>
          <!-- <div style="margin-left:52px;margin-bottom:24px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">产品名称</label>
            <el-select v-model="addMes.productId" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option v-for="item in allProduct" :key="item.product_id" :label="item.product_name"
                :value="item.product_id" />
            </el-select>
          </div>
          <div style="margin-left:52px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">员工姓名</label>
            <el-select v-model="addMes.employeeId" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option v-for="item in allEmployee" :key="item.employee_id" :label="item.employee_name"
                :value="item.employee_id" />
            </el-select>
          </div> -->
          <div style="margin-left:52px;margin-bottom:24px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">是否有机</label>
            <el-select v-model="addMes.isOrganic" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option label="是" :value="true" />
              <el-option label="否" :value="false" />
            </el-select>
          </div>
          <el-form-item class="item" label="肥料来源" :label-width="formLabelWidth">
            <el-input v-model="addMes.fertilizerSource" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="收购日期" :label-width="formLabelWidth">
            <el-input v-model="addMes.purchaseTime" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="备注" :label-width="formLabelWidth">
            <el-input v-model="addMes.remark" class="in" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDri()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column prop="fertilizerId" label="肥料信息id" width="180" />
        <el-table-column prop="fertilizerName" label="肥料名称" />
        <el-table-column prop="isOrganic" label="是否有机">
          <template scope="scope">
            {{ scope.row.isOrganic == true ? '是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="fertilizerSource" label="肥料来源" />
        <el-table-column prop="purchaseTime" label="收购日期" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="changeMsg(mes[scope.$index],scope.$index)">修改</el-button>
            <el-button type="text" style="color:red" @click="mes[scope.$index].isShowDel = true">删除</el-button>

            <el-dialog :modal-append-to-body="false" title="提示" :visible.sync="mes[scope.$index].isShowDel" width="30%">
              <span>确认要删除此条信息吗？</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="mes[scope.$index].isShowDel = false">取 消</el-button>
                <el-button type="primary" @click="driveDel(scope.$index)">确 定</el-button>
              </span>
            </el-dialog>

            <el-dialog
              :modal-append-to-body="false"
              title="修改信息"
              :visible.sync="mes[scope.$index].isShow"
              :before-close="handleClose"
            >
              <el-form class="form">
                <el-form-item class="item" label="肥料名称" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].fertilizerName" autocomplete="off" disabled />
                </el-form-item>
                <div style="margin-left:52px;margin-bottom:24px">
                  <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">是否有机</label>
                  <el-select v-model="mes[scope.$index].isOrganic" class="sel2" placeholder="请选择" style="width: 186px;">
                    <el-option label="是" :value="true" />
                    <el-option label="否" :value="false" />
                  </el-select>
                </div>
                <el-form-item class="item" label="肥料来源" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].fertilizerSource" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="收购日期" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].purchaseTime" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="备注" :label-width="formLabelWidth">
                  <el-input
                    v-model="mes[scope.$index].remark"
                    autocomplete="off"
                    type="textarea"
                    style="width: 350px;"
                  />
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeMsg(scope.$index)">取 消</el-button>
                <el-button type="primary" @click="changeInspectionForm(scope.$index,mes[scope.$index])">确 定</el-button>
              </div>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../utils/request'
export default {
  name: 'FarmlandSelection',
  data() {
    return {
      mes: [],
      firstMes: [],
      allMes: [],
      index: null,
      dialogTableVisible: false,
      formLabelWidth: '120px',
      addMes: {},
      dialogFormVisible: false,
      outerVisible: false,
      pageSize: null,
      pageNum: 1,
      deliverPageNum: 1,
      deliverPageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      total: null,
      allEmployee: [],
      allProduct: [],
      allCompany: []
    }
  },
  async mounted() {
    this.getDrive()
    // this.getEmployee()
    // this.getProduct()
    // this.getCompany()
  },
  methods: {
    async getDrive() {
      var a = JSON.stringify({})
      console.log(a)
      await request({
        method: 'post',
        url: '/plant/fer/' + this.pageNum + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        console.log(response, 'this.mes')
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
        this.pageSize = response.data.total
        this.total = response.data.total
      })
    },
    // async getEmployee () {
    //   await request({
    //     method: 'get',
    //     url: '/plant/employee/getAll'
    //   }).then((response) => {
    //     this.allEmployee = JSON.parse(JSON.stringify(response.data))
    //     // console.log(response, 'employee')
    //   })
    // },
    // async getProduct () {
    //   await request({
    //     method: 'get',
    //     url: '/plant/product/getAll'
    //   }).then((response) => {
    //     this.allProduct = JSON.parse(JSON.stringify(response.data))
    //     // console.log(response, '123123')
    //   })
    // },
    // async getCompany () {
    //   await request({
    //     method: 'get',
    //     url: '/plant/company/getAll'
    //   }).then((response) => {
    //     this.allCompany = JSON.parse(JSON.stringify(response.data))
    //     // console.log(response, '123123')
    //   })
    // },
    changeMsg(mesIndex, index) {
      mesIndex.isShow = true
      this.firstMes = JSON.parse(JSON.stringify(this.mes))[index]
      this.index = index
      console.log(this.mes[index], 'mesIndex')
      // console.log('this.firstMes', this.firstMes);
    },

    async changeInspectionForm(index, mes) {
      var changMes = JSON.parse(JSON.stringify(this.mes))[index]
      console.log(changMes, 'changMes000')
      await request({
        method: 'put',
        url: '/plant/fer/' + mes.fertilizerId,
        data: changMes
      }).then(response => {
        this.getDrive()
        if (response.statusCode == 20000) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
        this.mes[index].isShow = false
      })
    },
    cancelChangeMsg(index) {
      this.mes[index].isShow = false
      // console.log(111);
      this.mes[index] = JSON.parse(JSON.stringify(this.firstMes))
      console.log(this.mes[index])
      this.getDrive()
    },
    async driveDel(index) {
      await request({
        method: 'delete',
        url: '/plant/fer/' + this.mes[index].fertilizerId
      }).then(response => {
        console.log(response)
        this.getDrive()
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })
      this.mes[index].isShowDel = false
    },

    async addDri() {
      console.log('this.addMes', this.addMes)
      // for (let i = 0; i < this.allProduct.length; i++) {
      //   const e = this.allProduct[i];
      //   console.log(e, 'ee');
      //   if (e.product_id == this.addMes.productId) {
      //     this.addMes.productName = e.product_name
      //     console.log('this.addMesproduct_name', this.addMes);
      //   }
      // }
      // for (let i = 0; i < this.allCompany.length; i++) {
      //   const e = this.allCompany[i];
      //   // console.log(e, 'ee');
      //   if (e.company_id == this.addMes.companyId) {
      //     this.addMes.companyName = e.company_name
      //     // console.log('this.addMes', this.addMes);
      //   }
      // }
      // for (let i = 0; i < this.allEmployee.length; i++) {
      //   const e = this.allEmployee[i];
      //   // console.log(e, 'ee');
      //   if (e.employee_id == this.addMes.employeeId) {
      //     this.addMes.employeeName = e.employee_nameer
      //     console.log('this.addMes', this.addMes);
      //   }
      // }
      request({
        method: 'post',
        url: '/plant/fer',
        data: this.addMes
      }).then(response => {
        // console.log(response)
        if (response.statusCode == 20000) {
          this.addMes = {}
          this.getDrive()
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，添加失败',
            type: 'warning'
          })
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      // this.pageNum = val
      if (this.select == '') {
        request({
          method: 'post',
          url: '/plant/fer/' + val + '/5'
        }).then((response) => {
          // console.log(response)
          this.mes = response.data.records
          for (let i = 0; i < this.mes.length; i++) {
            this.$set(this.mes[i], 'isShow', false)
            this.$set(this.mes[i], 'isShowDel', false)
          }
        })
      } else if (this.select == 'fertilizerName') {
        var data = {
          fertilizerName: this.input3
        }
        request({
          method: 'post',
          url: '/plant/fer/' + val + '/5',
          data: data
        }).then((response) => {
          // console.log(response)
          this.mes = response.data.records
          for (let i = 0; i < this.mes.length; i++) {
            this.$set(this.mes[i], 'isShow', false)
            this.$set(this.mes[i], 'isShowDel', false)
          }
        })
      } else if (this.select == 'isOrganic') {
        var data = {
          isOrganic: this.input3 == '是'
        }
        request({
          method: 'post',
          url: '/plant/fer/' + val + '/5',
          data: data
        }).then((response) => {
          // console.log(response)
          this.mes = response.data.records
          for (let i = 0; i < this.mes.length; i++) {
            this.$set(this.mes[i], 'isShow', false)
            this.$set(this.mes[i], 'isShowDel', false)
          }
        })
      }
    },

    searchMes() {
      // console.log('this.mes', this.mes);
      if (this.input3) {
        if (this.select == 'fertilizerName') {
          var data = {
            fertilizerName: this.input3
          }
          request({
            method: 'post',
            url: '/plant/fer/1/5',
            data: data
          }).then((response) => {
            // console.log(response)
            if (response.statusCode == 20000) {
              this.$message({
                message: '查询成功',
                type: 'success'
              })
              this.mes = response.data.records
              console.log('response', response)
              for (let i = 0; i < this.mes.length; i++) {
                this.$set(this.mes[i], 'isShow', false)
                this.$set(this.mes[i], 'isShowDel', false)
              }
              this.pageSize = response.data.total
            } else {
              this.$message({
                message: '查无该农药，查询失败',
                type: 'warning'
              })
            }
          })
        } else if (this.select == 'isOrganic') {
          var data = {
            isOrganic: this.input3 == '是'
          }
          request({
            method: 'post',
            url: '/plant/fer/1/5',
            data: data
          }).then((response) => {
            // console.log(response)
            if (response.statusCode == 20000) {
              this.$message({
                message: '查询成功',
                type: 'success'
              })
              this.mes = response.data.records
              console.log('response', response)
              for (let i = 0; i < this.mes.length; i++) {
                this.$set(this.mes[i], 'isShow', false)
                this.$set(this.mes[i], 'isShowDel', false)
              }
              this.pageSize = response.data.total
            } else {
              this.$message({
                message: '查询失败',
                type: 'warning'
              })
            }
          })
        }
      } else {
        this.getDrive()
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addMes = {}
          this.cancelChangeMsg(this.index)
        })
        .catch(_ => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 350px;
  // border: 1px solid black;
  display: flex;
  margin-bottom: 20px;
  justify-content: space-between;
}

.form {
  display: flex;
  flex-wrap: wrap;
  // border: 1px solid black;
  // justify-content: space-around;
}

.sel {
  width: 360px;
  display: flex;
  height: 50px;
  // border: 1px solid black;
  // border: 1px solid black;
  justify-content: flex-end;
  align-content: center;
  // border: 1px solid black;
}

.sel2 {
  width: 240px;
}

.page {
  width: 100%;
  display: flex;
  justify-content: right;
}

.imgDri {
  height: 300px;
  width: 400px;
}

.w1 {
  height: 80px;
  font-size: 25px;
  font-weight: 350;
}

.w2 {
  height: 50px;
  font-size: 15px;
  font-weight: 250;
}

.searchHead {
  display: flex;
  justify-content: space-between;
}

.searchIuput {
  width: 350px;
}

.sel3 {
  width: 120px;
}
</style>

